from mysql.connector import pooling


class MySqlConnectionPool:
    def __init__(self, host="localhost", port=3306, username="root", password="", database_name="default", pool_size=5,
                 pool_name="mysql_pool"):
        self.host = host
        self.port = port
        self.username = username
        self.password = password
        self.database_name = database_name
        self.pool = pooling.MySQLConnectionPool(
            host=host,
            port=port,
            username=username,
            password=password,
            database=database_name,
            pool_name=pool_name,
            pool_size=pool_size,
        )

    def get_connection(self):
        return self.pool.get_connection()

    def query_data(self, query):
        connection = self.get_connection()
        cursor = connection.cursor()
        try:
            cursor.execute(query)
            result = cursor.fetchall()
            return result
        except Exception as e:
            print("Error: ", e)
        finally:
            cursor.close()
            connection.close()

    def exec_sql(self, query, params=None):
        connection = self.get_connection()
        cursor = connection.cursor()
        try:
            cursor.execute(query, params)
            connection.commit()
        except Exception as e:
            print("Error: ", e)
        finally:
            cursor.close()
            connection.close()
